package com.lexsoft.kanaiscube.data;

import java.math.BigDecimal;
import java.math.BigInteger;

import com.lexsoft.kanaiscube.R;
import com.lexsoft.kanaiscube.util.Tools;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class BossHitpoint extends Fragment implements OnClickListener,OnRatingBarChangeListener,OnFocusChangeListener{
	View view ;
	WebView table;
	int levelStart,levelEnd;
	double baseHitpoint,factor,powerBase,personIncrease;
	BigDecimal stoneMin,stoneMax,otherMin,otherMax,snakeMin,snakeMax;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		view = inflater.inflate(R.layout.fragment_boss_hitpoint, container,
				false);
		int[] initData = getResources().getIntArray(R.array.boss_hitpoints);
		baseHitpoint = initData[0]/100.0;
		factor = initData[1]/100.0;
		powerBase = initData[2]/100.0;
		personIncrease = initData[3]/100.0;
		stoneMin = new BigDecimal(initData[4]);
		stoneMax = new BigDecimal(initData[5]);
		otherMin = new BigDecimal(initData[6]);
		otherMax = new BigDecimal(initData[7]);
		snakeMin = new BigDecimal(initData[8]);
		snakeMax = new BigDecimal(initData[9]);
		levelStart = initData[10];
		levelEnd = initData[11];

		((RatingBar)view.findViewById(R.id.data_bosshitpoint_persons)).setOnRatingBarChangeListener(this);
		((EditText)view.findViewById(R.id.data_bosshitpoint_damage)).setOnFocusChangeListener(this);
		
		table = (WebView)view.findViewById(R.id.data_bosshitpoint_table);
		table.getSettings().setDefaultTextEncodingName("utf-8") ;
		table.setBackgroundColor(0);
		
		showTable();
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		showTable();
	}
	private void showTable(){
		StringBuffer sb = new StringBuffer();
		int rating = Math.round(((RatingBar)view.findViewById(R.id.data_bosshitpoint_persons)).getRating());
		String str = ((EditText)view.findViewById(R.id.data_bosshitpoint_damage)).getText().toString();
		BigDecimal damage = new BigDecimal(0);
		if(rating<=0){
			rating = 1;
		}
		if(!"".equals(str)){
			try{
				damage = new BigDecimal(str);
			}catch(Exception e){
			}
		}
		Log.d("lex",damage.toString());
		BigDecimal base = new BigDecimal(0.5*(1+rating)*baseHitpoint*factor);
		sb.append("<style type='text/css'>.td{border-bottom:1px solid #888}" +
				".itd{border-bottom:1px solid #444}" +
				"td{color:#808040}" +
				"th{border-bottom:1px solid #888;color:#c67000;position:relative;z-index:1}" +
				"</style>");
		sb.append("<table cellspacing=1 cellpadding=0>");
		sb.append("<tr><th nowrap align=center>");
		sb.append(getString(R.string.data_bosshitpoint_nephalem_level));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.data_bosshitpoint_bosstype));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.data_bosshitpoint_bosshit));
		sb.append("</th><th nowrap align=center>");
		sb.append(getString(R.string.data_bosshitpoint_elapse));
		sb.append("</th></tr>");
		for(int level=levelStart;level<=levelEnd;level++){
			BigDecimal levelFactor = new BigDecimal(Math.pow(powerBase, level-1));
			sb.append("<tr><td rowspan=3 align=center valign=center class=td>");
			sb.append(level);
			sb.append("</td><td align=center nowrap class=itd>");
			sb.append(getString(R.string.data_bosshitpoint_stoneman));
			sb.append("</td><td align=center class=itd>");
			BigDecimal stoneDamageMin = base.multiply(levelFactor).multiply(stoneMin);
			BigDecimal stoneDamageMax = base.multiply(levelFactor).multiply(stoneMax);
			sb.append(Tools.formatIntString(stoneDamageMin.toBigInteger().toString())+" - "+Tools.formatIntString(stoneDamageMax.toBigInteger().toString()));
			sb.append("</td><td align=center class=itd>");
			if(!damage.toBigInteger().equals(BigInteger.ZERO)){
				sb.append(Tools.toTimeString(stoneDamageMin.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger())+
						" - "+
						Tools.toTimeString(stoneDamageMax.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger()));
			}
			sb.append("</td></tr>");
			sb.append("<tr><td align=center nowrap class=itd>");
			sb.append(getString(R.string.data_bosshitpoint_others));
			sb.append("</td><td align=center class=itd>");
			BigDecimal otherDamageMin = base.multiply(levelFactor).multiply(otherMin);
			BigDecimal otherDamageMax = base.multiply(levelFactor).multiply(otherMax);
			sb.append(Tools.formatIntString(otherDamageMin.toBigInteger().toString())+" - "+Tools.formatIntString(otherDamageMax.toBigInteger().toString()));
			sb.append("</td><td align=center class=itd>");
			if(!damage.toBigInteger().equals(BigInteger.ZERO)){
				sb.append(Tools.toTimeString(otherDamageMin.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger())+
						" - "+
						Tools.toTimeString(otherDamageMax.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger())
						);
			}
			sb.append("</td></tr>");
			sb.append("<tr><td align=center nowrap class=td>");
			sb.append(getString(R.string.data_bosshitpoint_snakegirl));
			sb.append("</td><td align=center class=td>");
			
			BigDecimal snakeDamageMin = base.multiply(levelFactor).multiply(snakeMin);
			BigDecimal snakeDamageMax = base.multiply(levelFactor).multiply(snakeMax);
			sb.append(Tools.formatIntString(snakeDamageMin.toBigInteger().toString())+" - "+Tools.formatIntString(snakeDamageMax.toBigInteger().toString()));
			sb.append("</td><td align=center class=td>");
			if(!damage.toBigInteger().equals(BigInteger.ZERO)){
				sb.append(Tools.toTimeString(snakeDamageMin.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger())+
						" - "+
						Tools.toTimeString(snakeDamageMax.divide(damage,0,BigDecimal.ROUND_UP).toBigInteger())
						);
			}
			sb.append("</td></tr>");
		}
		sb.append("</table>");
		table.loadDataWithBaseURL("about:blank",sb.toString(),"text/html", "utf-8","");
	}

	@Override
	public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
		showTable();
	}

	@Override
	public void onFocusChange(View arg0, boolean arg1) {
		if(!arg1){
			showTable();
		}
		
	}

	
}
